package com.web.exiaodao.remote.service.impl;

import com.web.exiaodao.business.entity.RollTitleTable;
import com.web.exiaodao.remote.vo.RollTitleTableVo;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.web.exiaodao.business.repository.RollTitleTableRepository;
import com.web.exiaodao.remote.service.ApiRollTitleTableService;
import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.jpa.DynamicSpecifications;
import com.web.webstart.base.util.jpa.SearchFilter;
import com.web.webstart.base.service.impl.BaseService;
import com.web.webstart.base.util.jpa.SearchFilter.Operator;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.XaUtil;

/**
 * @author: zhangl
 * @times: 2015-5-15下午06:46:35
 * 类的说明：前端APIRollTitleTable接口实现类
 **/
@Service("ApiRollTitleTableService")
@Transactional(readOnly = false)
public class ApiRollTitleTableServiceImpl extends BaseService<RollTitleTable> implements ApiRollTitleTableService{

	private static final Logger LOGGER = Logger.getLogger(ApiRollTitleTableServiceImpl.class);

	@Autowired
	RollTitleTableRepository rollTitleTableRepository;
	
	@Override
	public XaResult<RollTitleTableVo> findOne(Long tId) throws BusinessException {
		RollTitleTable obj = rollTitleTableRepository.findByIdAndStatusNot(tId,XaConstant.Status.delete);
		XaResult<RollTitleTableVo> xr = new XaResult<RollTitleTableVo>();
		if (XaUtil.isNotEmpty(obj)) {
			xr.setObject(JSON.parseObject(JSON.toJSONString(obj),RollTitleTableVo.class));
			xr.getObject().setId(obj.getId());
			LOGGER.info("查询单个实体----->RollTitleTable");
		} else {
			xr.error(XaConstant.Message.object_not_find);
			LOGGER.info(XaConstant.Message.object_not_find);
		}
		return xr;
	}

	@Override
	public XaResult<List<RollTitleTableVo>> findListNEStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示非删除的所有数据
			status = XaConstant.Status.delete;
		}
		filters.put("status", new SearchFilter("status", Operator.NE, status));
		Page<RollTitleTable> page = rollTitleTableRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), RollTitleTable.class), pageable);
		XaResult<List<RollTitleTableVo>> xr = new XaResult<List<RollTitleTableVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), RollTitleTableVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		return xr;
	}

	@Override
	public XaResult<List<RollTitleTableVo>> findListEQStatusByFilter(
			Integer status, Map<String, Object> filterParams, Pageable pageable)
			throws BusinessException {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		if(status == null){// 默认显示正常数据
			status = XaConstant.Status.valid;
		}
		filters.put("status", new SearchFilter("status", Operator.EQ, status));
		Page<RollTitleTable> page = rollTitleTableRepository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), RollTitleTable.class), pageable);
		XaResult<List<RollTitleTableVo>> xr = new XaResult<List<RollTitleTableVo>>();
		xr.setObject(JSON.parseArray(JSON.toJSONString(page.getContent()), RollTitleTableVo.class));
		for(int i=0;i<page.getContent().size();i++){
			xr.getObject().get(i).setId(page.getContent().get(i).getId());
		}
		return xr;
	}

	@Override
	public XaResult<RollTitleTableVo> multiOperate(String modelIds,
			Integer status) throws BusinessException {
		XaResult<RollTitleTableVo> xr = new XaResult<RollTitleTableVo>();
		if(status == null){
			status = XaConstant.Status.delete;
		}
		if(modelIds != null){
			String[] ids = modelIds.split(",");
			for(String id : ids){
				RollTitleTable obj = rollTitleTableRepository.findByIdAndStatusNot(Long.parseLong(id),status);
				if (XaUtil.isNotEmpty(obj)) {
					obj.setStatus(status);
					obj = rollTitleTableRepository.save(obj);
					xr.setObject(JSON.parseObject(JSON.toJSONString(obj), RollTitleTableVo.class));
					xr.getObject().setId(obj.getId());
				}
			}
		}
		LOGGER.info("更改数据状态----->RollTitleTable");
		return xr;
	}

	@Override
	public XaResult<RollTitleTableVo> createModel(RollTitleTable model)
			throws BusinessException {
		XaResult<RollTitleTableVo> xr = new XaResult<RollTitleTableVo>();
		RollTitleTable obj = rollTitleTableRepository.save(model);
		xr.setObject(JSON.parseObject(JSON.toJSONString(obj), RollTitleTableVo.class));
		xr.getObject().setId(obj.getId());
		LOGGER.info("新增数据成功----->RollTitleTable");
		return xr;
	}

	@Override
	public XaResult<RollTitleTableVo> updateModel(RollTitleTable model)
			throws BusinessException {
		RollTitleTable obj = rollTitleTableRepository.findOne(model.getId());
		XaResult<RollTitleTableVo> xr = new XaResult<RollTitleTableVo>();
		if (XaUtil.isNotEmpty(obj)) {
					obj.setRollTitle_Content(XaUtil.isNotEmpty(model.getRollTitle_Content()) ? model.getRollTitle_Content() : obj.getRollTitle_Content());
				obj = rollTitleTableRepository.save(obj);
			xr.setObject(JSON.parseObject(JSON.toJSONString(obj), RollTitleTableVo.class));
			xr.getObject().setId(obj.getId());
		} else {
			xr.error(XaConstant.Message.object_not_find);
		}
		LOGGER.info("编辑数据成功----->RollTitleTable");
		return xr;
	}

}
